/**
 * 登录注册
 */
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
})

 $logintempl = {
     baseTemp:'<div class="windowlayout"><div class="layer_form_box"><div class="close_win" onclick="closeWin()"></div>{content}</div></div>',
     logintemp:'<div class="form_login_area"><div class="form_login_title"><h4>登录</h4><p>欢迎登录本站(⊙o⊙)</p></div><div class="form_login_row"><p>用户名</p><input type="text"></div><div class="form_login_row"><p>密码</p><input type="text"></div><div class="form_login_row"><span id="errtips"></span><input type="submit"value="登录" onclick="loginDo()"></div></div><div class="class_form_login_tips"><h4>还不是会员？</h4><p>您可以注册一个</p><a href="JavaScript:;"></a></div>',
     regstertemp:{
         step1:'<div class="form_login_area"><div class="form_login_title"><h4>注册</h4><p>欢迎注册本站(⊙o⊙)</p></div><div class="form_login_row"><p>用户名</p><input type="text"></div><div class="form_login_row"><p>密码</p><input type="text"></div><div class="form_login_row"><p>确认密码</p><input type="text"></div><div class="form_login_row"><span id="regerr_tips"></span><input type="submit"value="注册" onclick="RegiterDo()"></div></div><div class="class_form_login_tips"><h4>已是本站会员？</h4><p>去登录</p><a href="JavaScript:;"></a></div><div class="register_step"><span><a href="javascript:;"class="active"></a></span><span><a href="javascript:;"></a></span></div>',
         step2:'<div class="w_form step2"style="position: relative;"><div class="conpalre">完善基本信息</div><div class="w_c_form_row"><span>昵称：</span><input type="text"></div><div class="w_c_form_row"><span>QQ：</span><input type="text"></div><div class="w_c_form_row"><span>生日：</span><input type="text"placeholder="生日格式 如：1991-12-19"></div><div class="sub_login" onclick="RegiterDoStep2()">完成</div><div class="w_login_error"></div></div>',
     }
 }

 function showLoginWindow(op) {
     var windowLay = document.getElementsByClassName('windowlayout')[0];
     if (windowLay){
           document.body.removeChild(windowLay);
     }
     $hteml =  $logintempl.baseTemp;
     if (op=='login') {
         $title = "登录";
         $hteml = $hteml.replace(/\{content\}/,$logintempl.logintemp);
         document.body.innerHTML += $hteml;
     }else{
         $title = '注册';
         $hteml = $hteml.replace(/\{content\}/,$logintempl.regstertemp.step1);
         console.log($hteml);
         document.body.innerHTML += $hteml;
     }
     setTimeout(function () {
         $(".layer_form_box").css('marginTop','180px');
     },30)
 }

 function closeWin() {
     var windowLay = document.getElementsByClassName('windowlayout')[0];
     if (windowLay){
         document.body.removeChild(windowLay);
     }
 }

 function showError(tips) {
      $('#errtips').html(tips);
 }

/**
 * 登录处理
 */
function loginDo() {
      var input = document.getElementsByClassName('form_login_area')[0].getElementsByTagName('input');
      if (input[0].value=='' || input[1].value==''){
          showError('用户名或密码不能为空');
          return;
      }
      var data = {};
      data.username = input[0].value;
      data.password = input[1].value;
      $.ajax({
         type:"POST",
         url:"/passport/login",
         data:data,
          dataType:"json",
         success:function (data) {
             if (data['code'] == 200) {
                    window.location.reload();
             }else{
                    showError('用户名或密码错误');
             }
         }
      })
 }

/**
 * 注册处理 第一步·
 */
function RegiterDo() {
    var input = document.getElementsByClassName('w_login_form')[0].getElementsByTagName('input');
    if (input[0].value=='' || input[1].value==''){
        showError('账号和密码不能为空');
        return;
    }
    if (input[1].value !== input[2].value ){
         showError('两次输入的密码不一致');
         return;
    }
    var data = {};
    data.username = input[0].value;
    data.password = input[1].value;
    data.type     = 'step1';
    $.ajax({
        type:"POST",
        url:"passport/register-do",
        data:data,
        dataType:"json",
        success:function (data) {
            if (data['code'] == 200) {
                var windowLay = document.getElementsByClassName('windowlayout')[0];
                document.body.removeChild(windowLay);
                 // 第二步
                $hteml =  $logintempl.baseTemp;
                $title = '注册';
                $hteml = $hteml.replace(/\{method\}/,$title).replace(/\{content\}/,$logintempl.regstertemp.step2).replace(/\{tips\}/,'<div class="login—tips">已有账号？<a href="">立即登录</a></div>');
                document.body.innerHTML += $hteml;
            }else{
                showError('用户名或密码错误');
            }
        }
    })
}

/**
 * 注册处理
 */
function RegiterDoStep2() {
    var input = document.getElementsByClassName('w_form')[0].getElementsByTagName('input');
    if (input[0].value==''){
        showError('昵称不能为空');
        return;
    }
    var data = {};
    data.nickname = input[0].value;
    if (input[1].value) {
        data.birthday = input[1].value;
    }
    if (input[2].value) {
        data.qq = input[2].value;
    }
    data.type   = 'step2';
    $.ajax({
        type:"POST",
        url:"passport/register-do",
        data:data,
        dataType:"json",
        success:function (data) {
            if (data['code'] == 200) {
                 window.location.reload();
            }else{
                showError(data['msg']);
            }
        }
    })
}